package com.dnjn.happiness.emo.handler;


import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.system.api.RemoteConfigService;
import com.dnjn.happiness.system.api.RemoteDeptService;
import com.dnjn.happiness.system.api.RemoteUserService;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.emo.domain.EmoTeamAvgScoreHistory;
import com.dnjn.happiness.emo.domain.EmoTeamNumberOfCommHistory;
import com.dnjn.happiness.emo.service.IEmoTeamAvgScoreHistoryService;
import com.dnjn.happiness.emo.service.IEmoTeamNumberOfCommHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component("historyTaskHandler")
public class HistoryTaskHandler {

    @Autowired
    private IEmoTeamAvgScoreHistoryService emoTeamAvgScoreHistoryService;

    @Autowired
    private IEmoTeamNumberOfCommHistoryService emoTeamNumberOfCommHistoryService;

    @Autowired
    private RemoteDeptService deptService;

    @Autowired
    private RemoteUserService userService;

    @Autowired
    private RemoteConfigService configService;

    @Value("${happiness.daysAgo}")
    private Long daysAgo;


    /**
     * quartz：定时记录每个部门的所有员工的平均快乐值
     */
    public void recordingUserAvgScore() {
        List<Long> deptIdListAll = new ArrayList<>();
        R<List<Long>> result = deptService.getListAll(SecurityConstants.INNER);
        if(StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            deptIdListAll = result.getData();
        }
        for (Long deptId : deptIdListAll) {
            //所有子部门
            List<SysDept> deptChildList = new ArrayList<>();
            R<List<SysDept>> resultDeptList = deptService.getDeptListByDeptId(deptId,SecurityConstants.INNER);
            if(StringUtils.isNotNull(resultDeptList) && StringUtils.isNotNull(resultDeptList.getData())){
                deptChildList = resultDeptList.getData();
            }
            //把当前部门也加进去
            List<Long> deptIdList = deptChildList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            deptIdList.add(deptId);
            Long happinessScore = null;
            R<Long> resultByDeptIdList = userService.getUsersByDeptIdList(deptIdList, SecurityConstants.INNER);
            if(StringUtils.isNotNull(resultByDeptIdList) && StringUtils.isNotNull(resultByDeptIdList.getData())){
                happinessScore = resultByDeptIdList.getData();
            }
            if (ObjectUtils.isEmpty(happinessScore)) {
                happinessScore = 0L;
            }
            EmoTeamAvgScoreHistory emoTeamAvgScoreHistory = new EmoTeamAvgScoreHistory()
                    .setAvgScore(happinessScore)
                    .setCurrDate(LocalDate.now())
                    .setDeptId(deptId);
            //落库
            emoTeamAvgScoreHistoryService.save(emoTeamAvgScoreHistory);
        }
    }

    /**
     * quartz： 定时删除历史平均快乐值表中几天前的数据 (可配置 )
     */
    public void deleteAvgScoreHistoryByDaysAgo() {
        String delFlag = LocalDate.now().minusDays(daysAgo).toString();
        emoTeamAvgScoreHistoryService.deleteHistoryByDaysAgo(delFlag);
    }


    /**
     * 定时记录每个部门需沟通人数
     */
    public void recordingCommunicationCount() {
        //临界值
        Integer num = 0;
        R<String> result = configService.getThresholdDateaApi(SecurityConstants.INNER);
        if(StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            num = Integer.parseInt(result.getData());
        }

        List<Long> deptIdListAll = new ArrayList<>();
        R<List<Long>> resultLongList = deptService.getListAll(SecurityConstants.INNER);
        if(StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            deptIdListAll = resultLongList.getData();
        }
        for (Long deptId : deptIdListAll) {
            //所有子部门
            List<SysDept> deptChildList = new ArrayList<>();
            R<List<SysDept>> resultDeptList = deptService.getDeptListByDeptId(deptId,SecurityConstants.INNER);
            if(StringUtils.isNotNull(resultDeptList) && StringUtils.isNotNull(resultDeptList.getData())){
                deptChildList = resultDeptList.getData();
            }
            //把当前部门也加进去
            //所有子部门ID集合
            List<Long> deptIdList = deptChildList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            deptIdList.add(deptId);
            Long count = null;
            R<Long> resultCount = userService.getCommunicationCount(num, deptIdList, SecurityConstants.INNER);
            if(StringUtils.isNotNull(resultCount) && StringUtils.isNotNull(resultCount.getData())){
                count = resultCount.getData();
            }

            EmoTeamNumberOfCommHistory numberOfCommHistory = new EmoTeamNumberOfCommHistory()
                    .setCount(count)
                    .setCurrDate(LocalDate.now())
                    .setDeptId(deptId);
            //落库
            emoTeamNumberOfCommHistoryService.save(numberOfCommHistory);
        }
    }

    /**
     * 定时删除每个部门需沟通人数历史记录表
     */
    public void deleteNumberOfCommHistoryByDaysAgo() {
        String delFlag = LocalDate.now().minusDays(daysAgo).toString();
        emoTeamNumberOfCommHistoryService.deleteNumberOfCommHistoryByDaysAgo(delFlag);
    }

}
